import os
import threading

from flask import Flask
from flask_login import LoginManager

from config import config
from trace_logger import MiddlewareTraceLogger
from web.vo.User import User
from trace_logger.log import Logger

app = Flask(__name__)

evn_ = config[os.environ.get('FLASK_ENV')]
app.config.from_object(evn_)

from db import db

db.init_app(app)
app.secret_key = 'abc'  # 设置表单交互密钥
login_manager = LoginManager()  # 实例化登录管理对象
login_manager.init_app(app)
login_manager.login_view = 'custom.login'  # 设置用户登录视图函数 endpoint
login_manager.login_message = '请先登录或注册'

app.thread_local = threading.local()
MiddlewareTraceLogger().init_app(app)
log = Logger()
log.init_app(app)


# https://www.cnblogs.com/xujunkai/p/12364619.html
# https://segmentfault.com/a/1190000018087099


@login_manager.user_loader  # 定义获取登录用户的方法
def load_user(user_id):
    return User.getUserById(uid=user_id)




if __name__ == '__main__':

    from task import create_task
    create_task(app)


    from web import custom
    app.register_blueprint(custom)

    app.run(host='0.0.0.0', port=80)
